package com.zihan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zihan.constant.ParamsConstant;
import com.zihan.constant.UserConstant;
import com.zihan.context.UserContext;
import com.zihan.domain.dto.UserDTO;
import com.zihan.domain.entity.User;
import com.zihan.domain.result.PageResult;
import com.zihan.domain.result.Result;
import com.zihan.domain.vo.UserVO;
import com.zihan.mapper.UserMapper;
import com.zihan.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author zhangzihan
 * @since 2025-10-17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    /**
     * 获取用户列表信息
     * @param userDTO
     * @return
     */
    @Override
    public Result<PageResult> getList(UserDTO userDTO) {
        Integer pageNo = userDTO.getPageNo();
        Integer pageSize = userDTO.getPageSize();
        if (Objects.isNull(pageNo) || Objects.isNull(pageSize)) {
            return Result.error(ParamsConstant.LACK_PARAMS);
        }

        // 分页查询用户数据
        Page<User> page = lambdaQuery()
                .like(!StrUtil.isBlank(userDTO.getAccount()), User::getAccount, userDTO.getAccount())
                .like(!StrUtil.isBlank(userDTO.getUsername()), User::getUsername, userDTO.getUsername())
                .like(!StrUtil.isBlank(userDTO.getPhone()), User::getPhone, userDTO.getPhone())
                .eq(!Objects.isNull(userDTO.getRoleId()), User::getRoleId, userDTO.getRoleId())
                .eq(!Objects.isNull(userDTO.getStatus()), User::getStatus, userDTO.getStatus())
                .page(new Page<>(pageNo, pageSize));

        PageResult pageResult = new PageResult(page.getTotal(), page.getCurrent(), page.getRecords());
        return Result.success(pageResult);
    }



    /**
     * 重置密码
     * @param id
     * @return
     */
    @Override
    public Result<String> resetPassword(Long id) {
        // 将用户密码重置为手机号
        User user = lambdaQuery().eq(User::getId, id).one();
        if (user == null) {
            return Result.error(UserConstant.USER_NOT_EXIST);
        }
        // 将用户密码更新为手机号
        user.setPassword(DigestUtil.md5Hex(user.getPhone()));
        updateById(user);
        return Result.success();
    }

    @Override
    public Result<String> register(User user) {
        if (user == null) {
            return Result.error(ParamsConstant.LACK_PARAMS);
        }
        // 检查参数
        if (StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getAccount()) || StrUtil.isBlank(user.getPassword()) || StrUtil.isBlank(user.getPhone()) || StrUtil.isBlank(user.getPassword())) {
            return Result.error(ParamsConstant.LACK_PARAMS);
        }
        user.setPassword(DigestUtil.md5Hex(user.getPassword()));
        user.setStatus(UserConstant.USER_0);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        user.setDeleted(0);
        user.setAvatar(UserConstant.DEFAULT_AVATAR);
        save(user);
        return Result.success(UserConstant.USER_REGISTER_SUCCESS);
    }

    @Override
    public Result<String> updatePassword(UserDTO user) {
        Long currentId = UserContext.getCurrentId();
        User u = lambdaQuery().eq(User::getId, currentId)
                .eq(User::getPassword, DigestUtil.md5Hex(user.getOriginPassword()))
                .one();
        if (u == null) {
            return Result.error("原密码错误");
        }
        if (user.getNewPassword().equals(user.getOriginPassword())) {
            return Result.error("新旧密码不能相同");
        }
        // 更新密码
        u.setPassword(DigestUtil.md5Hex(user.getNewPassword()));
        updateById(u);
        return Result.success("修改密码成功");
    }
}
